Excel VBA for Complete Beginners: A Home and Learn Book by Carney Ken

Excel VBA for Complete Beginners: A Home and Learn Book by Carney Ken

Author:Carney, Ken [Carney, Ken]
Language: eng
Format: azw3, epub
Published: 2020-05-16T16:00:00+00:00


Each variable is separated by a comma. You can pass no variables, one variable, or as many as you need.

As well as specifying the variable names, you can specify a variable type, as well:

Sub SecondCode(BoldValue As Boolean, NameValue As String)

End Sub

If you miss out the "As Variable_Type" then the variables are treated As Variant . Notice that you don't need the Dim word anymore.

But this is just setting up the variable. They have nothing in them yet. To place something in these variables, you do so on the calling line.

Call SecondCode(True, "Arial")

Each value you place between the round brackets is known as an argument (or sometimes a parameter). The arguments must match. So if you have set up your Sub line to accept two arguments then you must pass two arguments in on the calling line, otherwise you'll get an error. The order of the arguments must match, as well. If the first variable is a Boolean, then you can't pass in a value of "Arial". Likewise, if the second argument is a String, then you can't pass in a number. Unless, that is, you set each variable up as Variants by missing off As String , As Boolean between the round brackets of the Sub line.

To clear that up, if our Sub is this:

Sub SecondCode(BoldValue As Boolean, NameValue As String)

Then this Calling line will get you an error:

Call SecondCode("value", "Arial")

But this won't

Call SecondCode(True, "Arial")

The error is because we have the text "value" as the first argument. The Sub says that BoldValue should be a Boolean, however. Boolean values can either be True or False , so VBA will give you a "Type Mismatch" error. (You can have 1 or 0 for your Booleans, though. A value of 1 means True and a value of 0 means False. You can even put the True and False in quote marks, and in any case you like: upper, lower, or a mix.)

In the Sub where your calling line is, you can set up variables, place values in the variables, and then type the variable names between the round brackets. Like this:

Dim boolValue As Boolean

Dim strText As String

boolValue = True

strText = "Arial"

Call SecondCode( boolValue, strText )

Now, we have variable names between the round brackets of the calling line. VBA will pass over to the SecondCode Sub whatever is inside of these variables. It will transfer the values to the new variables names:

Sub SecondCode(BoldValue As Boolean, NameValue As String)

So the value in boolValue will get transferred to the BoldValue variable, and the value in strText will get transferred to the NameValue variable.

Once you have values in the new variables between the round brackets of your Sub, you can do something with these variables.

To see how that works, let's now amend our SecondCode Sub to accept variables.

Change the first line of your code to this (all on oneline):

Private Sub SecondCode(BoldValue As Boolean, NameValue As String, SizeValue)

Here, we've set three arguments between the round brackets of SecondCode . The first is a Boolean value called BoldValue , and the second is a String value called NameValue .



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.